<?php

class Menu {
    
    private $modules = array();
    
    public function __construct($user) {
        foreach($user->getRights() as $mid => $lvl) {
            if($lvl > 0) {
                $module = new Module($mid);
                if($module->getMid() > 0) {
                    $this->modules[$mid] = $module->name;
                }
            }
        }
    }
    
    public function getMenu() {
        $menu = '<div class="menu">';
        foreach($this->modules as $mid => $name) {
            if(!$mid) {
                continue;
            }
            $menu .= '<a href="index.php?module=' . $mid . '"><button>' . $name . '</button></a>';
        }
        $menu .= '<a href="index.php?action=logout"><button>Zakończ pracę</button></a>';
        return $menu . '</div>';
    }
    
    public static function createMenu($buttons, $params, $mainPage=false, $maxInEachRow=array()) {
        if(isset($params['action'])) {
            unset($params['action']);
        }
        $menu = '<div class="menu">';
        $row = 0;
        $btns = 0;
        foreach($buttons as $key => $button) {
            if(count($maxInEachRow) > $row) {
                $btns++;
                if($btns > $maxInEachRow[$row]) {
                    $row++;
                    $btns = 1;
                    $menu .= '<br />';
                }
            }
            $menu .= '<a href="' . View::createLink($params) . '&action=' . $button['action'] . '"><button>' . $button['text'] . '</button></a>';
        }
        if($mainPage) {
            $menu .= '<a href="index.php"><button>Powrót do strony głównej</button></a>';
        }
        $menu .= '</div>';
        return $menu;
    }
}

?>
